<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	微站模版列表，模版列表显示所有模版，免费的模版可直接使用，付费模版需先购买
	
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>'0','type'=>3);
	if(isset($_RQ['group_id']) && $_RQ['group_id']!='all'){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}else{
		$return['params']['group_id']='all';
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$fields=array('id','title','group_id','image');
	$list=pdo_getslice('core_mobile_template',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	if($list){
		$group_templates=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'mobile_template');
		foreach($list as &$item){
			//根据模版价格，结合用户套餐、用户购买情况，判断是否需要购买
			$time=pdo_getcolumn('core_account_permissions',array('uniacid'=>$_SESSION['uniacid'],'type'=>2,'pid'=>$item['id']),'time');
			if($item['price'] && !in_array($item['id'],$group_templates) && (!$time || $time<TIMESTAMP)){
				$item['buy']=1;
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if(!$return['params']['type']){
		$return['params']['type']=0;
	}
	$return['groups']=get_template_groups();
	/*$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'公共模版库'),
			array('op'=>'my','title'=>'我的模版'),
			array('op'=>'add','title'=>'直接添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模版类型','type'=>'select','options'=>'groups'),
			array('op'=>'add','title'=>'使用模版','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);*/
	exi($return);
}
if($_RQ['op']=='my'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>3);
	if(isset($_RQ['group_id']) && $_RQ['group_id']!='all'){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}else{
		$return['params']['group_id']='all';
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$fields=array('id','title','group_id','image');
	$list=pdo_getslice('core_mobile_template',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if(!$return['params']['group_id']){
		$return['params']['group_id']=0;
	}
	$return['groups']=get_template_groups();
	/*$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'公共模版库'),
			array('op'=>'my','title'=>'我的模版'),
			array('op'=>'add','title'=>'直接添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'模版名称','type'=>'string'),
			array('name'=>'type','title'=>'模版类型','type'=>'select','options'=>'types'),
			array('op'=>'add','title'=>'使用模版','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);*/
	exi($return);
}
if($_RQ['op']=='add'){
	//添加微站
	model('core/web')->check_num('mobile');
	$id=intval($_RQ['id']);
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'type'=>3,
		'createtime'=>TIMESTAMP
	);
	if($id){
		//判断是否免费、是否购买
		//读取数据
		$data=pdo_get('core_mobile_template',array('id'=>$id),array('items','setting','title','pages','plugin_name','pageaction'));
		$pages= $data['pages']?json_decode($data['pages'],true):array();
		$insert['items']=$data['items'];
		$insert['setting']=$data['setting'];
		$insert['plugin_name']=$data['plugin_name'];
		$insert['pageaction']=$data['pageaction'];
	}
	pdo_insert('core_mobile_web',$insert);
	$web_id=pdo_insertid();
	if(!empty($pages)){
		foreach($pages as &$row){
			$row['uniacid']=$_SESSION['uniacid'];
			$row['webid']=$web_id;
			$row['createtime']=TIMESTAMP;
			pdo_insert('core_mobile_web',$row);
		}
	}
	$action=array('action'=>'website.mobile_web','op'=>'edit','query'=>'id='.$web_id);
	if($id){
		$message="使用模版[{$data['title']}]创建微站成功，准备进入详情设置页！";
	}else{
		$message="直接创建微站成功，准备进入微站详情设置页！";
	}
	exi($message,'success',$action);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要删除的模版！','error');
	}
	pdo_delete('core_mobile_template',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！','success',array('op'=>'my'));
}
